package com.zygh.interactiveplatform.module.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zygh.interactiveplatform.common.constant.EmployeeResponseCodeConst;
import com.zygh.interactiveplatform.common.domain.ResponseDTO;
import com.zygh.interactiveplatform.module.entity.IllegalVehicle;
import com.zygh.interactiveplatform.module.entity.V1Restriction;
import com.zygh.interactiveplatform.module.entity.WfcsTemp;
import com.zygh.interactiveplatform.module.entity.Wffh;
import com.zygh.interactiveplatform.module.entity.vo.WfdmVO;
import com.zygh.interactiveplatform.module.mapper.IllegalVehicleMapper;
import com.zygh.interactiveplatform.module.mapper.V1RestrictionMapper;
import com.zygh.interactiveplatform.module.mapper.WfcsTempMapper;
import com.zygh.interactiveplatform.module.mapper.WffhMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author lee
 * @since 2021-03-01
 */
@Service
public class IllegalVehicleServiceImpl extends ServiceImpl<IllegalVehicleMapper, IllegalVehicle> {
    @Resource
    private IllegalVehicleMapper illegalVehicleMapper;
    @Resource
    private V1RestrictionMapper v1RestrictionMapper;
    @Resource
    private WffhMapper wffhMapper;
    @Resource
    private WfcsTempMapper wfcsTempMapper;

    /**
     * 查询所有违法代码
     *
     * @return
     */
    public ResponseDTO findWfdm() {
        List<IllegalVehicle> illegalVehicles = illegalVehicleMapper.selectList(null);
        if (illegalVehicles != null) {
            return ResponseDTO.succData(illegalVehicles);
        }
        return ResponseDTO.wrap(EmployeeResponseCodeConst.EMPTY_INFORMATION_ERROR);
    }

    @DS("postgresql")
    public ResponseDTO findOriginalData(WfdmVO wfdmVO) {
        PageHelper.startPage(wfdmVO.getPageNum(), wfdmVO.getPageSize());
        String wfdm = wfdmVO.getWfdm();
        //限行
        if (wfdm.equals("13441")) {
            List<V1Restriction> explains = v1RestrictionMapper.selectList(Wrappers.lambdaQuery(V1Restriction.class)
                    .eq(StrUtil.isNotEmpty(wfdmVO.getWfdm()), V1Restriction::getWfdm, wfdmVO.getWfdm())
                    .eq(StrUtil.isNotEmpty(wfdmVO.getHphm()), V1Restriction::getHphm, wfdmVO.getHphm())
                    .ge(StrUtil.isNotEmpty(wfdmVO.getStartDate()),V1Restriction::getCjsj, wfdmVO.getStartDate())
                    .le(StrUtil.isNotEmpty(wfdmVO.getEndDate()),V1Restriction::getCjsj, wfdmVO.getEndDate())
            );
            PageInfo<V1Restriction> pageInfo = new PageInfo<>(explains);
            return ResponseDTO.succData(pageInfo);
        }
        //违法复合表
        if (wfdm.equals("6011") || wfdm.equals("3019")) {
            List<Wffh> wffhs = wffhMapper.selectList(Wrappers.lambdaQuery(Wffh.class)
                    .eq(StrUtil.isNotEmpty(wfdmVO.getWfdm()), Wffh::getWfdm, wfdmVO.getWfdm())
                    .eq(StrUtil.isNotEmpty(wfdmVO.getHphm()), Wffh::getHphm, wfdmVO.getHphm())
                    .ge(StrUtil.isNotEmpty(wfdmVO.getStartDate()), Wffh::getCjsj, wfdmVO.getStartDate())
                    .le(StrUtil.isNotEmpty(wfdmVO.getEndDate()), Wffh::getCjsj, wfdmVO.getEndDate()));
            PageInfo<Wffh> pageInfo = new PageInfo<>(wffhs);
            return ResponseDTO.succData(pageInfo);
        }
        //违法初筛表
        List<WfcsTemp> wfcsTemps = wfcsTempMapper.selectList(Wrappers.lambdaQuery(WfcsTemp.class)
                .eq(StrUtil.isNotEmpty(wfdmVO.getWfdm()),WfcsTemp::getWfdm, wfdmVO.getWfdm())
                .eq(StrUtil.isNotEmpty(wfdmVO.getHphm()),WfcsTemp::getHphm, wfdmVO.getHphm())
                .ge(StrUtil.isNotEmpty(wfdmVO.getStartDate()),WfcsTemp::getCjsj, wfdmVO.getStartDate())
                .le(StrUtil.isNotEmpty(wfdmVO.getEndDate()),WfcsTemp::getCjsj, wfdmVO.getEndDate()));
        PageInfo<WfcsTemp> pageInfo = new PageInfo<>(wfcsTemps);
        return ResponseDTO.succData(pageInfo);
    }
}
